﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DataModel.Mongo;
using DataModel.Models;
using MongoDB.Bson;

namespace DataModel.Test
{
    public class LessonGenerator
    {
        public IEnumerable<MongoLesson> GetMongoLessons(int count)
        {
            var lessons = new List<MongoLesson>(count);
            for (var i = 0; i < count; i++)
            {
                lessons.Add(new MongoLesson
                                {
                                    Subject = RandomString(8),
                                    Auditory = "111-4",
                                    EndDate = new BsonDateTime(new DateTime(2013,6,1)),
                                    StartDate = new BsonDateTime(new DateTime(2013, 1, 1)),
                                    StartTime = new BsonDateTime(new DateTime(2013, 1, 1)),
                                    EndTime = new BsonDateTime(new DateTime(2013, 1, 1)),
                                    Periodicity = 4,
                                    PeriodicityMask = 5,
                                    Professor = RandomString(5)
                                });
            }
            return lessons;
        }

        public IEnumerable<Lesson> GetSqlLessons(int count, UniversityPortalEntities entities)
        {
            var rand = new Random();
            var auditory = entities.Auditories.FirstOrDefault();
            var subject = entities.Subjects.FirstOrDefault();
            var professor = entities.Professors.FirstOrDefault();
            var group = entities.Groups.FirstOrDefault();
            var lessonType = entities.LessonTypes.FirstOrDefault();

            var lessons = new List<Lesson>(count);
            for (var i = 0; i < count; i++)
            {
                var lesson = new Lesson
                                 {
                                     ID = Guid.NewGuid(),
                                     Auditory = auditory,
                                     Subject = subject,
                                     Professor = professor,
                                     beginDate = new DateTime(2013,1,1),
                                     endDate = new DateTime(2013, 6,1),
                                     beginTime = RandomTime(),
                                     endTime = RandomTime(),
                                     LessonType = lessonType,
                                     weekDay = (short)rand.Next(1,6),
                                     semester = 7,
                                     periodicity = 4,
                                     periodicityMask = 5
                                 };
                lesson.Groups.Add(group);

                lessons.Add(lesson);
            }
            return lessons;
        }

        private static string RandomString (int size)
        {
            var builder = new StringBuilder();
            var random = new Random();
            for (int i = 0; i < size; i++)
            {
                char ch = Convert.ToChar(Convert.ToInt32(Math.Floor(26 * random.NextDouble() + 65)));
                builder.Append(ch);
            }

            return builder.ToString();
        }

        private static TimeSpan RandomTime()
        {
            var randomTest = new Random();
            var newSpan = new TimeSpan(0, randomTest.Next(7, 20), randomTest.Next(0, 11)*5, 0);
            return newSpan;
        }
    }
}
